package com.dingding.service.activity.core.biz.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.dto.shareplan.ReqSharePlanList;
import com.dingding.facde.activity.dto.shareplan.ResSharePlan;
import com.dingding.facde.activity.dto.shareplan.SharePlanDto;
import com.dingding.service.activity.core.biz.ISharePlanListService;
import com.dingding.service.activity.core.dao.IActivityDao;

/**
 * @Description:获取共享计划列表的service
 * @author fengshuonan
 * @date 2016年8月8日 下午3:08:46
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("sharePlanListService")
public class SharePlanListServiceImpl extends AbstractResponse<ResSharePlan, ReqSharePlanList>
		implements ISharePlanListService {

	@Resource(name = "activityDao")
	private IActivityDao activityDao;

	@Override
	@Transactional(readOnly = true)
	public ResponseEntity<ResSharePlan> getSharePlanList(String cityNo) {

		ResponseEntity<ResSharePlan> response = new ResponseEntity<ResSharePlan>();

		if(ToolUtil.isBlank(cityNo)){
			response.setCode(HttpStatus.SC_BAD_REQUEST);
			response.setErrorMessage("客户端请求参数为空");
			response.setPromptMessage("客户端请求参数为空");
			return response;
		}

		//获取共享计划列表
		this.getSharePlanList(response, cityNo);

		return response;
	}

	
	/**
	 * @Description:获取共享计划列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	private void getSharePlanList(ResponseEntity<ResSharePlan> response, String cityNo) {
		// 获取共享计划列表
		List<SharePlanDto> list = this.activityDao.getSharePlanList(cityNo);
		
		if(list == null){
			response.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			response.setErrorMessage("共享计划列表为空");
			response.setPromptMessage("共享计划列表为空");
			return;
		}else{
			
			// 创建共享计划列表响应Bean
			List<SharePlanDto> sharePlanDtos = new ArrayList<SharePlanDto>();
			
			if(list.size() > 0){
				
				for(int i=0;i<list.size();i++){
					
					SharePlanDto sharePlanDto = list.get(i);
					
					// 修改图片访问路径
					sharePlanDto.setBanner(ToolUtil.imageAddress(sharePlanDto.getBanner()));
					
					sharePlanDtos.add(sharePlanDto);
				}
				
			}
			
			response.setCode(HttpStatus.SC_OK);
			response.setErrorMessage("请求成功");
			response.setPromptMessage("请求成功");
			response.setResponseBody(new ResSharePlan(sharePlanDtos));
			return;
		}
	}

}
